﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service.Utils;
using MRGSP.SurveyMaster.Infra;

namespace MRGSP.SurveyMaster.WebUI.Helpers
{
    public static class SurveyHelpers
    {
        public static string Survey(this HtmlHelper htmlHelper, IEnumerable<Question> questions)
        {
            return IoC.Resolve<ISurveyBuilder>().Build(questions);
        }

        public static string SurveyCreator(this HtmlHelper htmlHelper, IEnumerable<Question> questions)
        {
            return IoC.Resolve<ISurveyCreateBuilder>().Build(questions);
        }

        public static MvcHtmlString Pagination(this HtmlHelper helper)
        {
            var c = helper.ViewContext.RouteData.Values["controller"].ToString();
            var a = helper.ViewContext.RouteData.Values["action"].ToString();
            var pageCount = (helper.ViewData.Model as IPageableInfo).PageCount;
            var pageIndex = (helper.ViewData.Model as IPageableInfo).PageIndex;
            return Pagination(helper, pageCount, pageIndex, c, a);
        }

        public static MvcHtmlString Pagination(this HtmlHelper helper, int pageCount, int pageIndex, string controller, string action)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            var sb = new StringBuilder();
            sb.Append("<div class='pagination'>");

            if (pageCount < 8)
                sb.Append(RenderButtons(1, pageCount, pageIndex, urlHelper, controller, action));
            else if (pageIndex < 5)
                sb.AppendFormat("{0} ... {1}", RenderButtons(1, 5, pageIndex, urlHelper, controller, action), RenderButton(pageCount, pageIndex, urlHelper, controller, action));
            else if (pageIndex > pageCount - 5)
                sb.AppendFormat("{0} ... {1}", RenderButton(1, pageIndex, urlHelper, controller, action),
                                RenderButtons(pageCount - 5, pageCount, pageIndex, urlHelper, controller, action));
            else
                sb.AppendFormat("{0} ... {1} ... {2}",
                                RenderButton(1, pageIndex, urlHelper, controller, action),
                                RenderButtons(pageIndex - 2, pageIndex + 2, pageIndex, urlHelper, controller, action),
                                RenderButton(pageCount, pageIndex, urlHelper, controller, action));

            sb.Append("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }

        private static string RenderButtons(int from, int to, int index, UrlHelper urlHelper, string controller, string action)
        {
            var s = new StringBuilder();
            for (var i = from; i <= to; i++)
            {
                s.Append(RenderButton(i, index, urlHelper, controller, action));
            }
            return s.ToString();
        }

        private static string RenderButton(int number, int index, UrlHelper urlHelper, string controller, string action)
        {
            if (index != number)
                return string.Format("<a href='{0}'>{1}</a>",
                                urlHelper.Action(action, controller, new { page = number }), number);

            return string.Format("<span class='current'>{0}</span>", number);
        }
    }
}
